const MILLI_SECOND_PREFIX = /fff/;
// tslint:disable:triple-equals
// tslint:disable:member-ordering
/**
 * String Utils.
 *
 * @export
 * @class DateUtils
 */
export class DateUtils {
    /**
     * Convert date object to format date string via native js.
     *
     * @static
     * @param {Date} date
     * @returns {string}
     *
     * @memberOf DateUtils
     */
    public static formatDate(date: Date): string {
        let formatString;
        formatString = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' '
            + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ' ' + date.getMilliseconds();
        return formatString;
    }

    /**
     * Convert CN date string to normal format.
     *
     * @static
     * @param {Date} date
     * @returns {string}
     *
     * @memberOf DateUtils
     */
    public static convertFromCN(cndateStr: string): string {
        const ChineseToDigitTable = new Map([
            ['零', 0],
            ['O', 0],
            ['Ο', 0],
            ['О', 0],
            ['○', 0],
            ['Ｏ', 0],
            ['0', 0],
            ['〇', 0],
            ['一', 1],
            ['二', 2],
            ['两', 2],
            ['三', 3],
            ['四', 4],
            ['五', 5],
            ['六', 6],
            ['七', 7],
            ['八', 8],
            ['九', 9],
            ['十', 10],
            ['十一', 11],
            ['十二', 12],
            ['十三', 12],
            ['十四', 12],
            ['十五', 12],
            ['十六', 12],
            ['十七', 12],
            ['十八', 12],
            ['十九', 12],
            ['二十', 12],
            ['二十一', 12],
            ['二十二', 12],
            ['二十三', 12],
            ['二十四', 12],
            ['二十五', 12],
            ['二十六', 12],
            ['二十七', 12],
            ['二十八', 12],
            ['二十九', 12],
            ['三十', 12],
            ['三十一', 12],
        ]);
        // tslint:disable-next-line:one-variable-per-declaration
        let year, month, day;
        if (cndateStr.indexOf('年') > -1) {
            year = cndateStr.split('年')[0];
            month = cndateStr.split('年')[1].split('月')[0];
            day = cndateStr.split('月')[1].split('日')[0];
        } else if (cndateStr.indexOf('月') > -1) {
            month = cndateStr.split('月')[0];
            day = cndateStr.split('月')[1].split('日')[0];
        } else {
            throw Error('DateError: no good date');
        }
        const fday = ChineseToDigitTable.get(day) == null ?
            day ? day : '1' :
            ChineseToDigitTable.get(day);
        const fmonth = ChineseToDigitTable.get(month) == null ? month : ChineseToDigitTable.get(month);
        let fyear = '';
        if (year != null) {
            year.split('').map((x) => {
                fyear += ChineseToDigitTable.get(x) ? ChineseToDigitTable.get(x) : x;
            });
        }
        if (fday !== null && fmonth != null && fyear != null) {
            return cndateStr.indexOf('年') > -1 ? fyear + '-' + fmonth + '-' + fday :
                new Date().getFullYear() + '-' + fmonth + '-' + fday;
        } else {
            throw Error('DateError: no good date');
        }
    }

    /**
     * date orderBy
     *
     * @static
     * @param {Date} date
     * @returns {string}
     *
     * @memberOf DateUtils
     */
    public static dateOrderBy(dates: string[]) {
        return dates.sort((a, b) => {
            return Date.parse(a) - Date.parse(b);
        });
    }

    /**
     * date orderBy desc
     *
     * @static
     * @param {Date} date
     * @returns {string}
     *
     * @memberOf DateUtils
     */
    public static dateOrderByDesc(dates: string[]) {
        return dates.sort((a, b) => {
            return Date.parse(b) - Date.parse(a);
        });
    }
}
